//
//  ViewController.m
//  RoachMeter
//
//  Created by samb on 04/18/13.
//  Copyright (c) 2013 Sam Brodkin. All rights reserved.
//

#import "ViewController.h"
#import "ContentUtil.h"

#import <AudioToolbox/AudioToolbox.h>


@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad
{

  //svn up
    [super viewDidLoad];
    self.scrollView.minimumZoomScale= 0.5;
    self.scrollView.maximumZoomScale=2.0;
    self.scrollView.zoomScale= 0.5;

    //detect taps for splatting
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
        self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:singleTap];
}

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
    CGPoint touchPoint=[gesture locationInView:self.imageView];
    NSLog(@"touchPoint = %@", NSStringFromCGPoint(touchPoint));
    [self playSystemSound:@"squish"];
    UIImageView *bloodSplatImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bloodsplat.png"]];
    bloodSplatImage.frame= CGRectMake(touchPoint.x, touchPoint.y, 20, 20);
    [self.imageView addSubview:bloodSplatImage];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return _imageView;
}


- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    NSLog(@"scale = %f", scale);
    NSString *htmlString;
    htmlString= [ContentUtil getHtmlContent:scale];
    [_webView loadHTMLString:htmlString baseURL:nil];
}

/**
* Open URLs in Safari
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    return YES;
}

-(void) playSystemSound:(NSString *)fileName {
//    if (self.soundsMuted) {
//        return;
//    }
    //has no volume control
    NSString *soundName = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
    //declare a system sound id
    SystemSoundID soundID;
    //Get a URL for the sound file
    NSURL *filePath = [NSURL fileURLWithPath:soundName isDirectory:NO];
    //Use audio sevices to create the sound
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
    //Use audio services to play the sound
    AudioServicesPlaySystemSound(soundID);
}



@end